package net.bplaced.streetfighter.utils.update;

import net.bplaced.streetfighter.lib.model.AppVersion;
import net.bplaced.streetfighter.lib.utils.RuntimeData;
import net.bplaced.streetfighter.lib.utils.RuntimeDataKey;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class UpdateInfoAdapter
        extends BaseAdapter {
    Activity activity;
    AppVersion[] versions;
    Integer currentVersion = 0;

    public UpdateInfoAdapter(Activity activity, AppVersion[] versions) {

        super();

        this.activity = activity;
        this.versions = versions;

        if (RuntimeData.getInstance().get(RuntimeDataKey.CURRENT_VERSION) != null) {
            this.currentVersion = (Integer) RuntimeData.getInstance().get(RuntimeDataKey.CURRENT_VERSION).getData();
        }
    }

    @Override
    public int getCount() {

        return this.versions.length;
    }

    @Override
    public AppVersion getItem(int position) {

        return this.versions[position];
    }

    @Override
    public long getItemId(int position) {

        return Integer.valueOf(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return this.getSimpleView(position, convertView, parent);
    }

    private final static LayoutParams LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT = new AbsListView.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    private final static LayoutParams LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT = new AbsListView.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    private View getSimpleView(int position, View convertView, ViewGroup parent) {

        final AppVersion version = this.getItem(position);

        final LinearLayout view = new LinearLayout(this.activity);
        view.setLayoutParams(UpdateInfoAdapter.LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT);
        view.setOrientation(1);

        if (position != 0) {

            String versionString = version.versionName;
            if (this.currentVersion == version.versionCode) {
                versionString += " [INSTALLIERT]";
            }

            final TextView versionNumberView = new TextView(this.activity);
            versionNumberView.setText(versionString);
            versionNumberView.setLayoutParams(UpdateInfoAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
            versionNumberView.setTypeface(null, Typeface.BOLD);
            view.addView(versionNumberView);
        }

        for (final String note : version.releaseNotes) {
            view.addView(this.createViewForReleaseNote(note));
        }

        return view;
    }

    private View createViewForReleaseNote(final String note) {

        final TextView noteView = new TextView(this.activity);
        noteView.setText(note);
        noteView.setLayoutParams(UpdateInfoAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
        noteView.setPadding(0, 0, 0, 10);
        return noteView;
    }

    public AppVersion getLatestVersion() {

        return this.getItem(0);
    }
}